//
//  UIColorExtension.swift
//  Intersection
//
//  Created by 好东家 on 2022/8/12.
//

import UIKit

extension UIColor{
    
// MARK: 颜色生成图片
    ///颜色生成图片
    public func image() -> UIImage {
        let rect = CGRect.init(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        self.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage.init()
        UIGraphicsEndImageContext()
        return image
    }
    
    /// RGB色
    public static func FromRGB(R:CGFloat ,G:CGFloat ,B:CGFloat) -> UIColor{
        return UIColor(red: R/255.0, green: G/255.0, blue: B/255.0, alpha: 1)
    }

    /// RGB色带透明度
    public static  func FromRGB(R:CGFloat ,G:CGFloat ,B:CGFloat ,A:CGFloat) -> UIColor{
        return UIColor(red: R/225.0, green: G/255.0, blue: B/255.0, alpha: A)
    }

    /// 从RGB代码获得颜色
    public static func FromHex(rgb:Int) ->UIColor {
        return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
                     green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
                      blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
                     alpha: 1.0)
    }

    ///从RGB代码获得颜色 带透明度
    public static func FromHex(rgb:Int, alpha:CGFloat) ->UIColor {
        return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
                     green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
                      blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
                     alpha: alpha)
    }

    
    
    
    
}
